﻿using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using YY.Admin.Core.LiveCharts2;

namespace YY.Admin.Core.Converter
{
    public class BrushToSolidColorPaintConverter : IValueConverter
    {
        public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter is not CusSolidColorPaint sourcePaint)
                return new SolidColorPaint(SKColors.Transparent);

            var skColor = ResolveColor(sourcePaint.CusColor);

            var targetPaint = new CusSolidColorPaint(skColor);

            MapProperties(sourcePaint, targetPaint);

            return targetPaint;
        }

        // 统一的颜色解析方法
        private SKColor ResolveColor(string? colorKey)
        {
            if (SKColor.TryParse(colorKey, out var parsedColor))
                return parsedColor;

            var resource = Application.Current.FindResource(colorKey);

            return resource switch
            {
                Color mediaColor => new SKColor(mediaColor.R, mediaColor.G, mediaColor.B, mediaColor.A),
                SolidColorBrush brush => new SKColor(brush.Color.R, brush.Color.G, brush.Color.B, brush.Color.A),
                _ => SKColors.Transparent,
            };
        }

        protected void MapProperties(CusSolidColorPaint sourcePaint, CusSolidColorPaint targetPaint)
        {
            targetPaint.IsAntialias = sourcePaint.IsAntialias;
            targetPaint.StrokeThickness = sourcePaint.StrokeThickness;
            targetPaint.StrokeCap = sourcePaint.StrokeCap;
            targetPaint.StrokeMiter = sourcePaint.StrokeMiter;
            targetPaint.StrokeJoin = sourcePaint.StrokeJoin;
            targetPaint.ImageFilter = sourcePaint.ImageFilter;
            targetPaint.PathEffect = sourcePaint.PathEffect;

            #pragma warning disable CS0618 // Type or member is obsolete
            if (sourcePaint.FontWeight != SKFontStyleWeight.Normal || sourcePaint.FontWidth != SKFontStyleWidth.Normal || sourcePaint.FontSlant != SKFontStyleSlant.Upright)
                targetPaint.SKFontStyle = new SKFontStyle(sourcePaint.FontWeight, sourcePaint.FontWidth, sourcePaint.FontSlant);
            #pragma warning restore CS0618 // Type or member is obsolete

            if (sourcePaint?.FontFamily is not null)
            {
                targetPaint.SKTypeface = SKTypeface.FromFamilyName(sourcePaint?.FontFamily);
            }
            else if (sourcePaint?.TypefaceMatchesChar is not null && sourcePaint.TypefaceMatchesChar.Length > 0)
            {
                targetPaint.SKTypeface = SKFontManager.Default.MatchCharacter(sourcePaint.TypefaceMatchesChar[0]);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
}
